/*汉字输入*/
function chineseInput(oa, ob) {
    var btn = $(oa);
    var flag = true;//用于汉字输入完成判断
    //在输入一段需要确认的文本如拼音to汉字、语音时会触发
    btn.on('compositionstart', function () {
        flag = false;
    });
    //在拼音选词完成、语音输入完毕时会触发
    btn.on('compositionend', function () {
        flag = true;
    });
    //绑定事件
    btn.on('input propertychange', function () {
        var tt = $(this);
        setTimeout(function () {
            if (flag) {
                ob(tt, tt.val());
            }
        }, 0)
    })
}
/* 私信 */
let sxUsed = {
    sxMyUser: {
        id: 66,
        name: '海市蜃楼',
        img: 'https://www.sssgo.com/data/upload/avatar/9e8657ccd248f50d4d2873042493ae51.png',
        level: 3
    },//我的信息
    sxUserList: [],//私下用户列表数据
    sxOtherUser: {},//当前私信对方信息
    sxOtherCurrent: 0,//当前私信用户 所在序号
    sxUserbox: $('.sx-lsul'),//私信用户列表 父元素
    sxCon: $('.sx-rc-box'),//私信记录父元素
    sxTotal: 500,//私信单词可输入文字数量
    sxAreaEl: '#sx-content',//私信输入框El
    sxArea: $('#sx-content'),//私信输入框
    sxSendEl: '#sx-send',//私信发送按钮
    sxSearch: '#sx-search',//私信用户搜索EL
    init: function (id) {
        chineseInput(sxUsed.sxAreaEl, sxUsed.sxInput);//私信输入
        chineseInput(sxUsed.sxSearch, sxUsed.sxSearchUser);//用户搜索
        sxUsed.sxSend();//点击私下发送
        sxUsed.bindUserClick();//点击私下发送
        this.initUserList(id)
    },
    //初始化用户列表
    initUserList: function (id) {
        let list = [{
            id: 1,
            name: '小王1',
            img: 'https://oss.sssgo.com/data/upload/game/20250630/68626988aec2a.png',
            level: 1,
            text: '阿斯顿撒旦'
        }, {
            id: 2,
            name: '小王2',
            img: 'https://oss.sssgo.com/upload/cdcloud2/20250710/404b37bf447d9d63f41ff2b832bfd9fa.png',
            level: 2,
            text: '阿斯顿撒旦'
        }, {
            id: 3,
            name: '小王3',
            img: 'https://oss.sssgo.com/upload/cdcloud2/20250627/80b2436a45efd2725ec3cea81bb7d2ef.png',
            level: 3,
            text: '阿斯顿撒旦'
        }];
        sxUsed.sxUserList = list;
        let html = '';
        //循环拼接
        for (let i = 0; i < list.length; i++) {
            let item = list[i];
            //判断当前用户
            if (item.id == id) {
                sxUsed.sxOtherUser = item;
                sxUsed.sxOtherCurrent = i;
            }
            //拼接html
            html += `<li class="sx-lsul-li flex-align ${item.id == id ? 'active' : ''}">
                    <div class="sx-lsul-u"><img src="${item.img}" alt=""></div>
                    <div class="sx-lsul-c">
                        <div class="sx-lsul-ct flex-align">
                            <div class="sx-lsul-cat">${item.name}</div>
                            <img class="sx-lsul-cbt" src="./sub/images/youquan/yv${item.level}0.png" alt="">
                            <img class="sx-lsul-cct" src="./sub/images/youquan/yv${item.level}1.png" alt="">
                        </div>
                        <div class="sx-lsul-cb">
                            <span>${item.text}</span>
                        </div>
                    </div>
                </li>`
        }
        sxUsed.sxUserbox.html(html);
        console.log('用户写入了')
        sxUsed.initSxRecord();

    },
    //初始化私下记录
    initSxRecord: function () {
        console.log("初始化 privatelog");
        let user = sxUsed.sxOtherUser;
        let params = {
            id: user.id
        }
        // $.ajax({
        //     type: "get",
        //     url: "https://api.github.com/users/sx-admin/gists",
        //     data: params,
        //     dataType: "json",
        //     success: function (res) { }
        // })
        //对方头像显示
        $('.sx-ru').html(`<div class="sx-ru-img"><img src="${user.img}" alt=""></div>
                        <div class="sx-ru-name">${user.name}</div>
                        <img class="sx-ru-cbt" src="./sub/images/youquan/yv${user.level}0.png" alt="">
                        <img class="sx-ru-cct" src="./sub/images/youquan/yv${user.level}1.png" alt="">`)
        //返回数据处理渲染
        let list = [{
            type: 1,
            content: '羊托不托不是托'
        }, {
            type: 2,
            content: '2025-01-21 10:21'
        }, {
            type: 3,
            content: '羊托不是托羊托不是托羊托阿斯顿发生不是托羊托不是托羊托不是托'
        }, {
            type: 1,
            content: '羊托不是托是托羊托不是托羊托不是托'
        }, {
            type: 2,
            content: '2025-01-21 10:21'
        }];
        let html = '';
        //循环拼接
        for (let i = 0; i < list.length; i++) {
            let ctm = '';
            let item = list[i];
            //对方
            if (item.type == 1) {
                html += `<div class="sx-rca clearfix">
                        <div class="sx-rui"><img src="${user.img}" alt=""></div>
                        <div class="sx-rux">${item.content}</div>
                    </div>`
            }
            //时间
            if (item.type == 2) {
                html += `<div class="sx-rcb"><span>${item.content}</span></div>`
            }
            //自己
            if (item.type == 3) {
                html += `<div class="sx-rcc clearfix">
                        <div class="sx-rui"><img src="${sxUsed.sxMyUser.img}" alt=""></div>
                        <div class="sx-rux">${item.content}</div>
                    </div>`
            }
        }
        sxUsed.sxCon.html(html);
        sxUsed.setSxNum('');
        sxUsed.sxArea.val('');
        sxUsed.goDivBottom('');//初始化滚动到底部
    },
    //点击左侧用户
    bindUserClick: function () {
        $(document).on('click', '.sx-lsul-li', function () {
            let index = $(this).index();
            if (!$(this).hasClass('active')) {
                sxUsed.sxOtherUser = sxUsed.sxUserList[index];
                sxUsed.sxOtherCurrent = index;
                sxUsed.initSxRecord();
                $(this).addClass('active').siblings().removeClass('active');
            }
        })
    },
    //私信显示字数
    setSxNum: function (val) {
        $('#sx-num').text(val.length + '/' + sxUsed.sxTotal)
    },
    //私信记录滚动到底部
    goDivBottom: function (div) {
        var scrollableDiv = $('#scrollableDiv');
        scrollableDiv.scrollTop(scrollableDiv[0].scrollHeight);
    },
    //用户搜索
    sxSearchUser: function (el, val) {
        let html = '';
        let list = sxUsed.sxUserbox.children();
        for (let i = 0; i < list.length; i++) {
            let item = $(list[i]);
            if (item.find('.sx-lsul-cat').text().indexOf(val) > -1) {
                item.show()
            } else {
                item.hide()
            }
        }
    },
    //私信输入
    sxInput: function (el, val) {
        // console.log(val)
        if (val.length > sxUsed.sxTotal) {
            layer.msg('最多输入' + sxUsed.sxTotal + '个字');
            el.val(val.slice(0, sxUsed.sxTotal))
        }
        sxUsed.setSxNum(val)
    },
    //点击私信发送
    sxSend: function () {
        $(document).on('click', sxUsed.sxSendEl, function () {
            let content = sxUsed.sxArea.val();
            if (!content) {
                layer.msg('请输入私信内容');
            }
            else if (content.length > sxUsed.sxTotal) {
                layer.msg('私信内容不能超过' + sxUsed.sxTotal + '个字');
            }
            else {
                let data = {
                    content: content,
                    to_id: $('#sx-to_id').val(),
                    from_id: $('#sx-from_id').val(),
                    type: $('#sx-type').val(),
                };
                // $.ajax({
                // })
                //成功写入
                let html = `<div class="sx-rcc clearfix">
                        <div class="sx-rui"><img src="${sxUsed.sxMyUser.img}" alt=""></div>
                        <div class="sx-rux">${content}</div>
                    </div>`
                $('.sx-rc-box').append(html);
                sxUsed.goDivBottom();
                sxUsed.sxArea.val('');
            }
        });

    }
}

//分页状态
function pageState(list, data, limit = 10) {
    //list：列表所有数据条数
    //limit：每页多少条
    //data：本次请求返回多少条数据
    let status;
    if (list == 0) {
        status = 'noData';
    } else if (list % limit > 0 || (list % limit == 0 && data == 0)) {
        status = 'noMore';
    } else {
        status = 'more';
    }
    return status;
}
/* 编辑关注游圈 */
let yqGzUaList = [{
    id: '1',
    name: '张三',
    guanzhu: 155,
    fans: 525,
    isGz: false,
    img: 'https://www.sssgo.com/data/upload/avatar/9e8657ccd248f50d4d2873042493ae51.png',
}];//已选择关注游圈列表
let yqGzUbList = [];//搜索游戏列表
//搜索游戏
// $(document).on('click', '#search-btn', function () {
//     let val = $('#search-val').val();
//     $.ajax({
//         type: "get",
//         url: "https://api.github.com/users/" + val,
//         data: {},
//         dataType: "json",
//         success: function (res) {

//         }
//     })
//     //返回处理
//     let list = [{
//         id: '1',
//         name: '张三',
//         guanzhu: 155,
//         fans: 525,
//         isGz: false,
//         img: 'https://www.sssgo.com/data/upload/avatar/9e8657ccd248f50d4d2873042493ae51.png',
//     }, {
//         id: '2',
//         name: '张覆盖三',
//         guanzhu: 55,
//         fans: 55,
//         isGz: false,
//         img: 'https://www.sssgo.com/data/upload/avatar/9e8657ccd248f50d4d2873042493ae51.png',
//     }]
//     yqGzUbList = list;
//     let html = '';
//     for (let i = 0; i < list.length; i++) {
//         let item = list[i];
//         let id = item.id;
//         //已关注
//         if (yqGzUaList.some(its => its.id == id)) {
//             item.isGz = true;
//         }

//         html += `<li class="flex-align ${item.isGz ? 'on' : ''}"" data-id="${item.id}">
//                 <div class="yq-rmbx-ul-img">
//                     <img src="${item.img}" alt="">
//                 </div>
//                 <div class="yq-rmbx-ul-info flex1 ovn">
//                     <h4 class="ellipsis">${item.name}</h4>
//                     <div class="yq-rmbx-us">${item.fans}新帖</div>
//                     <div class="yq-rmbx-ub">${item.guanzhu}万人关注</div>
//                     <div class="yq-rmbx-ua ${item.isGz ? 'on' : ''}">${item.isGz ? '已关注' : '关注'}</div>
//                 </div>
//             </li>`
//     }
//     $('#rmbx-box').html(html);
// })
//点击搜索的游戏
// $(document).on('click', '#rmbx-box li', function () {
//     let id = $(this).attr('data-id');
//     let index = $(this).index();
//     let item = yqGzUbList[index];//当前选择的游戏
//     if ($(this).hasClass('on')) {
//         console.log('取消关注')
//     } else {
//         console.log('取消关注')
//     }
// })
